package com.example.youxuehu.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * BASE64加密解密
 *
 * @author youxuehu on 2019/10/6
 */
public class BASE64Utils {

    public static String encode(String str) {
        try {
            return new BASE64Encoder().encode(str.getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    public static String decode(String str) {
        try {
            return new String(new BASE64Decoder().decodeBuffer(str), "utf-8");
        } catch (IOException e) {
            return null;
        }

    }

    public static void main(String[] args) {
        System.out.println(encode("hello"));
        System.out.println(decode("aGVsbG8="));
    }
}
